<?php
namespace app\common\server;
use x\Xbase;
class User extends Xbase{

    /**
     * 注册会员 - 小程序
     * @param  [type] $phone      [手机号]
     * @param  [type] $openid     [openid]
     * @param  [type] $invitecode [邀请码]
     * @return [type]             [description]
     */
    public function bind($phone,$openid,$invitecode=''){
        $openid = $this->db->query("select id from user where openid=?",[$openid],1);
        if ($openid) return $this->setError('该openid已绑定！');

        $info = $this->db->query("select id from user where phone=?",[$phone],1);
        if ($info) {
            //绑定
            if ($info['openid']) return $this->setError('该手机号已被绑定！');

            $data = [
                'id' => $info['id'],
                'openid' => $openid
            ];
            if (!$info['s_id'] && $invitecode) {
                $s_userid = de_id($invitecode);
                $s_user = $this->db->find('user',$s_userid,'id,s_tree,s_level,s_nums');
                if (!$s_user) return $this->setError('错误的邀请码！');
                $data['s_id'] = $s_user['id'];
                $data['s_tree'] = $s_user['s_tree'].$s_user['id'].',';
                $data['s_level'] = $s_user['s_level']+1;
            }
            $this->db->update('user',$data);
            return $info['id'];
        }else{
            //注册
            $reg_jifen = c('rate.reg_jifen');
            $order_no = $this->com_order_no();
            $order_no1 = $this->com_order_no();

            $this->db->begin();
            $mysql = [];

            $data = [
                'username' => substr(md5($phone), 8, 16),
                'openid' => $openid,
                'phone' => $phone,
                's_id' => 0,
                's_tree' => ',0,',
                's_level' => 1,
            ];
            $s_user = 0;
            if ($invitecode) {
                $s_userid = de_id($invitecode);
                $s_user = $this->db->find('user',$s_userid,'id,s_tree,s_level,s_nums');
                if (!$s_user) return $this->setError('错误的邀请码！');
                $data['s_id'] = $s_user['id'];
                $data['s_tree'] = $s_user['s_tree'].$s_user['id'].',';
                $data['s_level'] = $s_user['s_level']+1;
            }
            $mysql[] = $id = $this->db->insert("user",$data);

            $data = [
                'order_no' => $order_no,
                'user_id' => $id,
                'money_jf' => $reg_jifen['user'],
                'create_time' => time()
            ];
            $mysql[] = $order_id = $this->db->insert('order_jifen',$data);
            #获得积分
            $mysql[] = m('Userbill','xcom')->doit($data['order_no'],$data['user_id'],105,'order_jifen',$order_id,$data['money_jf'],'money_jf','');
            #更新推荐数
            if ($s_user) {
                $mysql[] = $this->db->update("user",['id'=>$s_user['id'],'s_nums'=>$s_user['s_nums']+1]);
                #获得积分
                $data = [
                    'order_no' => $order_no1,
                    'user_id' => $s_user['id'],
                    'money_jf' => $reg_jifen['s_user'],
                    'create_time' => time()
                ];
                $mysql[] = $order_id = $this->db->insert('order_jifen',$data);
                $mysql[] = m('Userbill','xcom')->doit($data['order_no'],$data['user_id'],105,'order_jifen',$order_id,$data['money_jf'],'money_jf','');
            }
            
            $rs = $this->db->end($mysql);
            if(!$rs) return $this->setError('绑定失败');
            return $id;
        }        
    }


    /**
     * 注册会员 - app
     * @param  [type] $phone      [手机号]
     * @param  [type] $password   [密码]
     * @param  [type] $invitecode [邀请码]
     * @return [type]             [description]
     */
    public function register($phone,$password,$invitecode=''){
        $info = $this->db->query("select id from user where phone=?",[$phone]);
        if($info) return $this->setError('该手机号已存在！');

        $reg_jifen = c('rate.reg_jifen');
        $order_no = $this->com_order_no();
        $order_no1 = $this->com_order_no();
        $this->db->begin();
        $mysql = [];

        $data = [
            'username' => substr(md5($phone), 8, 16),
            'phone' => $phone,
            'password' => $this->com_password($password),
            's_id' => 0,
            's_tree' => ',0,',
            's_level' => 1,
        ];
        $s_user = 0;
        if ($invitecode) {
            $s_userid = de_id($invitecode);
            $s_user = $this->db->find('user',$s_userid,'id,s_tree,s_level,s_nums');
            if (!$s_user) return $this->setError('错误的邀请码！');
            $data['s_id'] = $s_user['id'];
            $data['s_tree'] = $s_user['s_tree'].$s_user['id'].',';
            $data['s_level'] = $s_user['s_level']+1;
        }
        $mysql[] = $id = $this->db->insert("user",$data);

        $data = [
            'order_no' => $order_no,
            'user_id' => $id,
            'money_jf' => $reg_jifen['user'],
            'create_time' => time()
        ];
        $mysql[] = $order_id = $this->db->insert('order_jifen',$data);
        #获得积分
        $mysql[] = m('Userbill','xcom')->doit($data['order_no'],$data['user_id'],105,'order_jifen',$order_id,$data['money_jf'],'money_jf','');
        #更新推荐数
        if ($s_user) {
            $mysql[] = $this->db->update("user",['id'=>$s_user['id'],'s_nums'=>$s_user['s_nums']+1]);
            #获得积分
            $data = [
                'order_no' => $order_no1,
                'user_id' => $s_user['id'],
                'money_jf' => $reg_jifen['s_user'],
                'create_time' => time()
            ];
            $mysql[] = $order_id = $this->db->insert('order_jifen',$data);
            $mysql[] = m('Userbill','xcom')->doit($data['order_no'],$data['user_id'],105,'order_jifen',$order_id,$data['money_jf'],'money_jf','');
        }

        $rs = $this->db->end($mysql);
        if(!$rs) return $this->setError('注册失败');
        return true;
    }

    //加密方式
    public function com_password($password,$salt=''){
        return md5(md5($password).$salt);
    }

}
